package edu.gdkm.collection;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class StudentManagerBySet {
    //Set是一个无序集合，可以用于放置元素，但是无法设置顺序
    Set<Student> studentSet =new HashSet<Student>();
    //定义一个初始化方法，读取文件score中的信息
    public void init() throws IOException {
        File file = new File(".\\doc\\score.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String s;
        //读取一行数据，通过分隔符切出6段记录，分别放入Student这个实体类的对象中
        //最后将这个对象存入集合studentList中去
        while ((s = br.readLine()) != null) {
            String[] records = s.split(",");
            Student student = new Student();
            student.setSname(records[0]);
            student.setSnumber(records[1]);
            student.setScore1(Double.parseDouble(records[2]));
            student.setScore2(Double.parseDouble(records[3]));
            student.setScore3(Double.parseDouble(records[4]));
            student.setScore4(Double.parseDouble(records[5]));
            studentSet.add(student);//将对象存入到集合中去
        }
    }
    public void printStudents(){
        //使用Iterator遍历集合,Iterator是集合遍历的专用接口，效率较高
        Iterator iterator=studentSet.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
    //添加一个学生
    public void addStudent(Student student){
        studentSet.add(student);
    }
    public Student findStudentBySnumber(String snumber){
//        for (int i = 0; i < studentSet.size(); i++) {
//            //i为下标，类似数组students[i]
//            Student student = studentSet.get(i);
//            if(snumber.equals(student.getSnumber())){
//                return student;
//            }
//        }
//        set没有get方法，无法通过下标获取对象，其元素只能通过Iterator遍历获得
        Iterator<Student> iterator=studentSet.iterator();
        while(iterator.hasNext()){
            Student student=iterator.next();
            if(snumber.equals(student.getSnumber())){
                return student;
            }
        }
        return null;
    }
}
